/*
 *  Copyright 2010 Farent-Solutions Ltd.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package com.farent.solutions.plugins.aribaweb.awl.completion;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.netbeans.modules.xml.schema.completion.spi.CompletionContext;
import org.netbeans.modules.xml.schema.completion.spi.CompletionModelProvider;
import org.netbeans.modules.xml.schema.completion.spi.CompletionModelProvider.CompletionModel;
import org.netbeans.modules.xml.schema.model.SchemaModel;
import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
import org.netbeans.modules.xml.xam.ModelSource;
import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author user
 */
//@org.openide.util.lookup.ServiceProvider(service=org.netbeans.modules.xml.schema.completion.spi.CompletionModelProvider.class)
public class AWLCompletionModelProvider  /**extends CompletionModelProvider */
{

//    @Override
    public List<CompletionModel> getModels( CompletionContext cc ) {
        List<CompletionModel> models = new ArrayList<CompletionModel>();

        SchemaModel a_model = createMetaSchemaModel("a-api.xsd");
        CompletionModel a_cm = new AWLCompletionModelEx( AwlCompletionContext.A_NS,
                AwlCompletionContext.A_PREFIX, a_model);
        models.add(a_cm);

        SchemaModel t_model = createMetaSchemaModel("t-api.xsd");
        CompletionModel t_cm = new AWLCompletionModelEx( AwlCompletionContext.T_NS,
                AwlCompletionContext.T_PREFIX, t_model);
        models.add(t_cm);

        SchemaModel w_model = createMetaSchemaModel("w-api.xsd");
        CompletionModel w_cm = new AWLCompletionModelEx( AwlCompletionContext.W_NS,
                AwlCompletionContext.W_PREFIX, w_model);
        models.add(w_cm);

        return models;
    }

    private SchemaModel createMetaSchemaModel( String schemaFile ) {
        try {
            InputStream in = getClass().getResourceAsStream( schemaFile ); //NOI18N
            javax.swing.text.Document d = AbstractDocumentModel.
                    getAccessProvider().loadSwingDocument(in);
	    ModelSource ms = new ModelSource(Lookups.singleton(d), false);
            SchemaModel m = SchemaModelFactory.getDefault().createFreshModel(ms);
            m.sync();
            return m;
        } catch (Exception ex) {
            //just catch
        }
        return null;
    }

}
